package com.ziyun.pop.mall.module.user.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.order.api.AddressService;
import com.service.order.exception.OrderException;
import com.service.order.model.address.AddressParamDto;
import com.service.order.model.address.AddressResultDto;
import com.service.utils.push.umeng.JsonResult;
import com.utility.JsonUtils;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * 用户地址Controller
 * 
 * @author lvai 9.15
 */
@Controller
public class UserAddressController {
	private static Logger _log = LoggerFactory.getLogger(UserAddressController.class);
	private static final String MODEL = "/user/userAddress";

	@Autowired
	private AddressService userAddressService;
	
	@Autowired
	private HttpRequestLocal httpRequestLocal;

	// @Autowired
	// private SdbEctoolsRegionsService sdbEctoolsRegionsService;

	/**
	 * 获取地址信息列表
	 * 
	 * @return
	 * @throws IOException
	 */
	@GetMapping(MODEL + "/index.do")
	public ModelAndView index() throws IOException {
		ModelAndView view = new ModelAndView("/user/userAddress/index.html");
		return view;
	}

	/**
	 * 刪除地址
	 * 
	 * @param addressId
	 * @return
	 * @throws OrderException
	 */
	@PostMapping(MODEL + "/removeAddress.json")
	@ResponseBody
	public JsonResult<?> removeAddress(Integer addressId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("addressId", addressId);
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		userAddressService.removeAddress(userId,map);
		return JsonResult.success();
	}

	/**
	 * 根据id获取地址信息
	 * 
	 * @param addressId
	 * @return
	 */
	@PostMapping(MODEL + "/getAddressInfoById.json")
	@ResponseBody
	public JsonResult<AddressResultDto> getAddressInfoById(Integer addressId) {
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		AddressResultDto addressResultDto = userAddressService.getAddressInfoById(userId,addressId).getData();
		return JsonResult.success(addressResultDto);
	}

	/**
	 * 获取用户所有地址信息
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/listAddressInfo.json")
	@ResponseBody
	public JsonResult<Map<String, Object>> listAddressInfo() {
		Map<String, Object> map = new HashMap<String, Object>();
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		List<AddressResultDto> listUserAddress = userAddressService.listAddressInfo(userId).getData();
		map.put("listUserAddress", listUserAddress);
		int addressCount;
		if (listUserAddress != null && listUserAddress.size() > 0) {
			addressCount = listUserAddress.size();
		} else {
			addressCount = 0;
		}
		map.put("addressCount", addressCount);
		return JsonResult.success(map);
	}

	/**
	 * 新增修改地址信息
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/saveOrUpdateAddress.json")
	@ResponseBody
	public JsonResult<?> saveOrUpdateAddress(String addressData) {
		AddressParamDto addressParamDto = JsonUtils.string2Object(addressData, AddressParamDto.class);
		// addressParamDto.setArea(sdbEctoolsRegionsService.getAreaByRegionId(addressParamDto.getAreaId()));
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		userAddressService.saveOrUpdateAddress(userId,addressParamDto);
		return JsonResult.success();
	}

	/**
	 * 修改地址为默认地址
	 * 
	 * @param addressId
	 * @return
	 */
	@PostMapping(MODEL + "/updateDefAddr.json")
	@ResponseBody
	public JsonResult<?> updateDefAddr(Integer addressId) {
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		AddressResultDto addressResultDto = userAddressService.getAddressInfoById(userId,addressId).getData();
		AddressParamDto addressParamDto = new AddressParamDto();
		BeanUtils.copyProperties(addressResultDto, addressParamDto);
		if (addressResultDto.getDefAddr()) {// 默认改非默认
			addressParamDto.setDefAddr(false);
		} else {// 非默认改默认
			addressParamDto.setDefAddr(true);
		}
		addressParamDto.setAddressId(addressId);
		userAddressService.saveOrUpdateAddress(userId,addressParamDto);
		return JsonResult.success();
	}

}
